package com.ericsson.epc.modules.userebm.task;

import com.ericsson.common.quartz.task.ManualTask;
import com.ericsson.common.util.*;
import com.ericsson.epc.modules.userebm.entity.CollectDeploy;
import com.ericsson.epc.modules.userebm.entity.EbmIpImsiMongo;
import com.ericsson.epc.modules.userebm.service.UserEbmService;

import java.util.*;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

/**
 * 清洗话单中的imis和IP地址的关系
 */
@Component
public class UserEbmTask implements ManualTask {

    private Logger log = LoggerUtils.Logger(LogFileName.UserEbmJob);

    @Autowired
    @Qualifier("userEbmService")
    private UserEbmService userEbmService;
    @Override
    public void execute(){
        List<CollectDeploy> collectDeployList=userEbmService.getCollectList();
        if(collectDeployList.size()>0) {
            String formatter="yyyy-MM-dd HH:mm:00";
            //当前时间
            String programDate= DateUtils.getPastDate(1,formatter);
            log.info("UserEbmTask execute start");
            //处理话单
            Map<String, Set<String>> result= userEbmService.dealUserEbm(collectDeployList,programDate);
            //处理数据到mongodb
            List<EbmIpImsiMongo> ebmIpImsiMongos=new ArrayList<>();
            EbmIpImsiMongo ebmIpImsiMongo;
            Iterator<Map.Entry<String, Set<String>>> it = result.entrySet().iterator();
            while(it.hasNext()){
                Map.Entry<String, Set<String>> entry = it.next();
                ebmIpImsiMongo=new EbmIpImsiMongo();
                ebmIpImsiMongo.setIp(entry.getKey());
                ebmIpImsiMongo.setImsiList(entry.getValue());
                ebmIpImsiMongos.add(ebmIpImsiMongo);
            }
            log.info("UserEbmTask insert mongodb start");
            userEbmService.batchInsertEbmIpImsiDataList(ebmIpImsiMongos,programDate);
            log.info("UserEbmTask insert mongodb end");
            log.info("UserEbmTask execute end");
        }
    }
}
